package stackdemo;

import java.util.Arrays;

/**
 * @Author 12629
 * @Description：
 */
public class MyStack {

    public int[] elem;
    public int usedSize;

    public MyStack() {
        elem = new int[10];
    }

    public void push(int val) {
        //1. 如果满了进行扩容
        if(isFull()) {
            elem = Arrays.copyOf(elem,elem.length*2);
        }
        //2. 才可以入栈
        elem[usedSize] = val;
        //3. us++
        usedSize++;
    }
    public boolean isFull() {
        return usedSize == elem.length;
    }

    public int pop() {
        if(isEmpty()) {
            return -1;
        }
        int oldVal = elem[usedSize-1];
        usedSize--;
        return oldVal;
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }

    public int peek() {
        if(isEmpty()) {
            return -1;
        }
        return elem[usedSize-1];
    }

    public int getUsedSize() {
        return usedSize;
    }
}
